﻿using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 一起来击键
{
    public partial class 文本输入窗体 : Form
    {
        DateTime startTime;
        int s = 0;                              //定义已用时间
        int b = 0;                               //定义总时间
        string str = "";
        string user_input = "";
        string[] shuzu = new string[10];//定义存储每一页文本的数组
        string[] shuzu_input = new string[10];//定义存储每一页文本的数组
        string[] duanluo = new string[100];
        int enter = 0;
        int flag_duan = 0;
       
        private int sudu_ = 0, time_;

        ArrayList shumalist = new ArrayList();  //定义存储再现过程的内容
        ArrayList timelist = new ArrayList();  //定义存储再现过程中的时刻信息

        int len1 = 1, len2 = 0;                 //定义两个与计时相关的标志
        int flag_begin = 0;                     //定义计时开始标志
        int flag_bn = 0;                        //定义重新开始的标志
        int flag_reappear = 0;                  //定义再现过程的标志
        int flag_time_stop = 1;                 //定义停止计时的标志

        public 文本输入窗体()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            ThreadStart childref3 = new ThreadStart(jisuan);  //开启计算线程
            Thread childThread3 = new Thread(childref3);
            childThread3.Start();

            ThreadStart childref2 = new ThreadStart(reappear);  //开启再现过程线程
            Thread childThread2 = new Thread(childref2);
            childThread2.Start();
        }

        private void button1_Click(object sender, EventArgs e)//点击开始按钮，选择文本
        {
            int t;
            if (checkBox1.Checked == false)
                t = 99999;
            else
                t = (int)numericUpDown1.Value;
            b = t * 60;

            flag_time_stop = 0;

            shumalist.Clear();//清空动态数组
            timelist.Clear();

            if (flag_bn != 0)                       //标签
            {
                MessageBox.Show("已重新开始");
            }
            user_input = "";         //定义当前富文本框用户输入的内容
            sudu_ = 0;
            time_ = 0;              //
            len1 = 1;
            len2 = 0;                 //定义两个与计时相关的标志
            flag_duan = 0;

            richTextBox2.Text = "";
            s = 0;
            flag_bn = flag_bn+1;

            OpenFileDialog dialog = new OpenFileDialog();  //实例化打开文件的方法
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文本文件";
            dialog.Filter = "所有文件(*.*)|*.*";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;

                using (StreamReader sr = new StreamReader(file, Encoding.Default))
                {
                    str = sr.ReadToEnd();
                }

            }

            richTextBox1.Text = str;
            文本排列();


          

        }

        private void 文本排列()   //分段存储
        {        
            string str2 = "";          
            for (int i = 0; i <str.Length; i++)
            {
                str2 = str2 + str[i].ToString();
                if (str[i] == '\n'|| i== str.Length-1)
                {
                    duanluo[flag_duan] = str2;                
                    flag_duan += 1;
                    str2 = "";
                }
            }          
        } 
           
        private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)  //键盘输入监控
        {
            if (e.KeyChar ==32)         //当用户输入空格时，则再追加一个空格，两个英文空格=一个中文空格
                richTextBox2.AppendText(e.KeyChar.ToString());
            if (e.KeyChar == 13)        //当用户输入回车时，enter标记加1
                enter =enter+ 1;
            string str4 = "";
            for (int i = enter; i <= flag_duan; i++)            
                str4 += duanluo[i];            
            richTextBox1.Text=str4;



            int enter_tem = 0; //回车键临时计数
            foreach (char c in richTextBox2.Text)   //获取用户当前文本的回车的个数，并对原文区进行排版
            {
                if (c == '\n')
                    enter_tem = enter_tem + 1;
            }
            label27.Text = enter_tem.ToString();
            enter = enter_tem;
           

            str4 = "";
            for (int i = enter; i <= flag_duan; i++)
                str4 += duanluo[i];
            richTextBox1.Text = str4;

            


        }      

        private void richTextBox2_TextChanged(object sender, EventArgs e)//若用户输入，则开始计时
        {
            len2 = user_input.Length;
            if (len2 == 2)
            {                         //如果用户开始输入，则开始计时，第一个字符为空格，用户输入一个字符后，总字符为2时开始计时
                flag_begin = flag_begin + 1;       //避免重复开始计时
                startTime = DateTime.Now;
            }
            if (flag_begin == 1)                  //lflag_begin为开始计时的标志                                                      
                len1 = len2;                     //len1为计时累加的标志 

            user_input = richTextBox2.Text;


            shumalist.Add(user_input);                            //记录用户输入的过程
            timelist.Add(ExecDateDiff(startTime, DateTime.Now));    //记录用户输入的时间
            startTime = DateTime.Now;



        }


        void jisuan()  //计算时间，计算组数，算出速度
        {
            while (true)
            {
                while (s <= b & flag_time_stop == 0)
                {
                    time_ = s;
                    label24.Text = "用时：" + s + " 秒";

                    if (checkBox1.Checked == false)
                        label25.Text = "剩余：∞ 秒";
                    else
                        label25.Text = "剩余：" + (b - s) + " 秒";
                    //double sudu = get_zushu(user_input) / (s + 0.0001) * 360; 
                    double sudu = user_input.Replace(" ", "").Replace("\n", "").Length / (s + 0.0001) * 360;
                    label26.Text = "速度：" + ((int)sudu).ToString();
                    sudu_ = ((int)sudu);
                    //label28.Text = "组数：" + get_zushu(user_input).ToString();
                    label28.Text = "字数：" + user_input.Replace(" ", "").Replace("\n", "").Length.ToString();
                    Thread.Sleep(999);
                    if (len2 != 0)
                    {
                        s = s + 1;
                    }
                }
                Thread.Sleep(1000);  //检测
            }
            MethodInvoker mi = new MethodInvoker(show_check);
            BeginInvoke(mi);
        }

        void show_check()//显示校验窗口
        {
            文本测试结果窗体 form2 = new 文本测试结果窗体(user_input, str, time_);
            form2.Show();
        }

        private void 小泽文本_Load(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)//按下校对按钮
        {
            flag_time_stop = 1;
            len2 = 0;
            show_check();
        }

        private void label27_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)//按下再现过程按钮
        {
            flag_reappear = 1;
        }

        void reappear()//再现过程线程
        {
            while (true)
            {
                if (flag_reappear == 1)
                {
                    richTextBox2.Text = "";
                    int times = shumalist.Count;
                    int temp = flag_bn;
                    for (int i_re = 0; i_re < times; i_re++)
                    {
                        if (temp != flag_bn)        //如果再现过程的时候按下了开始按钮，则退出再现过程
                            break;
                        int time000 = Convert.ToInt32(timelist[i_re]);
                        Thread.Sleep(time000);
                        richTextBox2.Text = (string)shumalist[i_re];
                    }
                    flag_reappear = 0;
                }
                Thread.Sleep(1000);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        double ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
        {
            TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
            TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();

            //你想转的格式
            return ts3.Milliseconds;
        }
    }
}
